package com.erpanderp.wam.tm.model.service;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.tm.model.entity.RRFManagerRecommendation;
import com.erpanderp.wam.tm.model.entity.RecruitmentProfile;
import com.erpanderp.wam.util.HibernateUtil;

public class RecruitmentProfileDAO {
	
	 public List<RecruitmentProfile> getAllRecruitmentProfiles(String technology,long formExperirnce,long toExperience){ 
		 List<RecruitmentProfile> profiles ;	
		
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				String queryString = "from RecruitmentProfile r where r.technology= :technology and candidateExperience >= :formExperirnce and candidateExperience <= :toExperience";
				
				Query query = session.createQuery(queryString).setString("technology", technology).setFloat("formExperirnce", formExperirnce).setFloat("toExperience", toExperience);
				query.setCacheable(false);
				profiles = (List<RecruitmentProfile>) query.list();
				System.out.println("COUNT OF profiles : "+profiles.size());
				session.getTransaction().commit();
				
				return profiles;
				
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}	
		}
	 
	 public List<RecruitmentProfile> getAllRecruitmentProfilesByStatus(String technology,long formExperirnce,long toExperience,String projectStatus){ 
		 List<RecruitmentProfile> profiles ;	
		
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				String queryString = "from RecruitmentProfile r where r.technology= :technology and candidateExperience >= :formExperirnce and candidateExperience <= :toExperience and r.profileCurrentStatus =:projectStatus";
				
				Query query = session.createQuery(queryString).setString("technology", technology).setFloat("formExperirnce", formExperirnce).setFloat("toExperience", toExperience).setString("projectStatus", projectStatus);
				query.setCacheable(false);
				profiles = (List<RecruitmentProfile>) query.list();
				System.out.println("COUNT OF profiles : "+profiles.size());
				session.getTransaction().commit();
				
				return profiles;
				
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}	
		}
	 
	 public boolean saveRecruitmentProfiles(List<RecruitmentProfile> recruitmentProfileList){
		boolean recruitmentProfileCreated = false;
		try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.AUTO);
			
				for(RecruitmentProfile recruitmentProfile : recruitmentProfileList){
					session.saveOrUpdate(recruitmentProfile);
				}
				session.getTransaction().commit();
				recruitmentProfileCreated = true;
			}catch(Exception e){
				e.printStackTrace();
				return recruitmentProfileCreated;
			}
			return recruitmentProfileCreated;
		}	
	 
	 public boolean saveRecruitmentProfile(RecruitmentProfile recruitmentProfile){
			boolean recruitmentProfileSaved = false;
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.AUTO);				
					session.saveOrUpdate(recruitmentProfile);				
				session.getTransaction().commit();
				recruitmentProfileSaved = true;
			}catch(Exception e){
				e.printStackTrace();
				return recruitmentProfileSaved;				
			}
			return recruitmentProfileSaved;
	}	
	 
	 
	 public List<RecruitmentProfile> getRecruitmentProfilesByRRFCode(String rrfCode){ 
		 List<RecruitmentProfile> profiles ;	
		System.out.println("rrfCode=========="+rrfCode);
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				String queryString = "from RecruitmentProfile r where r.recruitmentRequestCode= :rrfCode and r.shortListed='Yes'";
				
				Query query = session.createQuery(queryString).setString("rrfCode", rrfCode);
				query.setCacheable(false);
				profiles = (List<RecruitmentProfile>) query.list();
				System.out.println("COUNT OF profiles : "+profiles.size());
				session.getTransaction().commit();
				System.out.println("IN DAO====profiles sisze======== "+profiles.size());
				return profiles;
				
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}	
		}
	 
	 public List<RecruitmentProfile> getALLApprovedRRFProfilesByRRFCode(String rrfCode){ 
		 List<RecruitmentProfile> profiles ;	
		System.out.println("rrfCode=========="+rrfCode);
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				String queryString = "from RecruitmentProfile r where r.recruitmentRequestCode= :rrfCode and (r.approverFeedbackStatus='Approved' or r.approverFeedbackStatus='On Hold')";
				
				Query query = session.createQuery(queryString).setString("rrfCode", rrfCode);
				query.setCacheable(false);
				profiles = (List<RecruitmentProfile>) query.list();
				System.out.println("COUNT OF profiles : "+profiles.size());
				session.getTransaction().commit();
				System.out.println("IN DAO====profiles sisze======== "+profiles.size());
				return profiles;
				
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}	
		}
	
	 public List<RecruitmentProfile> getAllProfilesByRRFCode(String rrfCode){ 
		 List<RecruitmentProfile> profiles ;	
		System.out.println("rrfCode=========="+rrfCode);
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				String queryString = "from RecruitmentProfile r where r.recruitmentRequestCode= :rrfCode";
				
				Query query = session.createQuery(queryString).setString("rrfCode", rrfCode);
				query.setCacheable(false);
				profiles = (List<RecruitmentProfile>) query.list();
				System.out.println("COUNT OF profiles : "+profiles.size());
				session.getTransaction().commit();
				System.out.println("IN DAO====profiles sisze======== "+profiles.size());
				return profiles;
				
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}	
		}
	 
	 public RecruitmentProfile getRecruitmentProfileByCandidateEmail(String candidateEmail){
		 	RecruitmentProfile recruitmentProfile = new RecruitmentProfile();
		 	try{
		 		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		 		session.beginTransaction();
		 		session.setFlushMode(FlushMode.AUTO);
		 		String queryString = "from RecruitmentProfile rp where rp.candidateEmail = :candidateEmail";
		 		Query query = session.createQuery(queryString).setString("candidateEmail", candidateEmail);
		 		query.setCacheable(false);
		 		recruitmentProfile = (RecruitmentProfile) query.uniqueResult();
		 		System.out.println("recruitmentProfile=====OBJ===="+recruitmentProfile);
		 		session.getTransaction().commit();
		 		return recruitmentProfile;
		 	}catch (Exception e) {
		 		e.printStackTrace();
		 		return null;			
				}
		 	
		 }
	 
	 public RRFManagerRecommendation getRRFManagerRecommendationByProfileId(Long recruitmentProfileId,String rrfCode){
		 RRFManagerRecommendation rrfManagerRecommendation = new RRFManagerRecommendation();
		 	try{
		 		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		 		session.beginTransaction();
		 		session.setFlushMode(FlushMode.AUTO);
		 		String queryString = "from RRFManagerRecommendation mr where mr.recruitmentProfile.recruitmentProfileId = :recruitmentProfileId and mr.recruitmentRequestCode=:rrfCode";
		 		Query query = session.createQuery(queryString).setLong("recruitmentProfileId", recruitmentProfileId).setString("rrfCode", rrfCode);
		 		query.setCacheable(false);
		 		rrfManagerRecommendation = (RRFManagerRecommendation) query.uniqueResult();
		 		System.out.println("recruitmentProfile=====OBJ===="+rrfManagerRecommendation);
		 		session.getTransaction().commit();
		 		return rrfManagerRecommendation;
		 	}catch (Exception e) {
		 		e.printStackTrace();
		 		return null;			
				}
		 	
		 }
	
	 public RecruitmentProfile getRecruitmentProfileByProfileId(Long recruitmentProfileId){
		 	RecruitmentProfile recruitmentProfile = new RecruitmentProfile();
		 	try{
		 		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		 		session.beginTransaction();
		 		session.setFlushMode(FlushMode.AUTO);
		 		String queryString = "from RecruitmentProfile rp where rp.recruitmentProfileId = :recruitmentProfileId";
		 		Query query = session.createQuery(queryString).setLong("recruitmentProfileId", recruitmentProfileId);
		 		query.setCacheable(false);
		 		recruitmentProfile = (RecruitmentProfile) query.uniqueResult();
		 		System.out.println("recruitmentProfile=====OBJ===="+recruitmentProfile);
		 		session.getTransaction().commit();
		 		return recruitmentProfile;
		 	}catch (Exception e) {
		 		e.printStackTrace();
		 		return null;			
			}
		 	
		 }
	 
	 public RecruitmentProfile isRecruitmentProfileExists(String email,String mobileNo) {
		 RecruitmentProfile recruitmentProfile = null;
			try {
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				Query query = session.createQuery("from RecruitmentProfile rf where rf.candidateEmail=:email or rf.candidateMobileNumber=:mobileNo").setString(
						"email", email).setString("mobileNo", mobileNo);
				query.setCacheable(false);
				List<RecruitmentProfile> tempList = query.list();
				session.getTransaction().commit();
				
				if(tempList != null && tempList.size()>0){
					recruitmentProfile = tempList.get(0);
					return recruitmentProfile;
				}else{
					return null;
				}
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}		
		}
	 
public static void main(String a[]){
	//String technology,long formExperirnce,long toExperience,String projectStatus
	System.out.println(new RecruitmentProfileDAO().isRecruitmentProfileExists("ssss@erp.com","868686868"));
}
}
